<template>
  <div style="width: 180px;float: right;color: white;position: fixed;top: 20px;right: 0;z-index: 2;">
    <Select />
  </div>
  <div class="bg">
    <div class="form">
      <div class="logo">
        <img :src="logo" alt="">
      </div>
      <div class="box1">
        <div class="inner inner1">
          <el-input v-model="form.email" :placeholder="$t('电子邮箱')" style="width: 100%;margin-bottom: 5px;" />
          <div style="
          height: 32px;
          border-radius: 5px;
          font-size: 12px;
          background-color: #f6d39c;
          cursor: pointer;
          margin-left: auto;
          border:none;
          float: right;
          display: inline-block;
           padding-left: 5px;
           padding-right: 5px;
           padding-top: 8px;
           box-sizing: border-box;
        " class="bt" @click="startCountdown">
            <span v-if="countdown > 0">{{ countdown }}{{ $t('秒') }}</span>
            <span v-else>{{ $t('发送验证码') }}</span>
          </div>
        </div>
        <div class="inner inner2" style="margin-top: 40px;">
          <el-input v-model="form.code" :placeholder="$t('邮箱验证码')" />
        </div>
        <div class="inner inner3">
          <div style="display: flex;">
            <el-input v-model="inputCode" style="width: 210px; margin-right: 10px;" :placeholder="$t('验证码')" />
            <div :class="'code'" @click="createCode">{{ code }}</div>
          </div>
        </div>
        <div class="inner inner4">
          <el-input v-model="form.password" :placeholder="$t('密码')" type="password" show-password />
        </div>
        <div class="inner inner5">
          <el-input v-model="password" :placeholder="$t('确认密码')" type="password" show-password />
        </div>
        <div class="inner inner6">
          <div style="
          width: 100%;
          height: 45px;
          border-radius: 5px;
          background-color: #f6d39c;
          display: flex;
          justify-content: center;
          align-items: center;
          border:none;
          cursor: pointer;
        " class="bt" @click="submit">
            <span>{{ $t('提交') }}</span>
          </div>
        </div>
        <router-link to="/login" class="inner inner7">
          <span style="color: #fff; font-size: 14px; "> {{ $t('已有账号？请登录') }} </span>
        </router-link>
      </div>
    </div>
    <div class="find">
      <span style="color: #f6d39c;">{{ $t('通过邮箱找回') }} </span>
    </div>
  </div>
</template>

<script>
//import { userLogin, getAuth1, getAuth2 } from '@/api/user'
import Select from '@/components/select.vue'
import { ElNotification } from 'element-plus'
import {
  sendEmailApi,
  userFindApi,
  settingApi
} from '@/api'
export default {
  name: 'LoginPage',
  components: {
    Select
  },
  data() {
    return {
      countdown:0,
      logo:'',
      form: {
        email: '',
        code: '',
        password: '',
      },
      password: '',
      inputCode: '',
      code: '',
    }
  },
  methods: {
    // 60s倒计时
    startCountdown() {
      if (this.countdown === 0) {
        this.getEmailCode();
        this.countdown = 60;
        const timer = setInterval(() => {
          if (this.countdown > 0) {
            this.countdown--;
          } else {
            clearInterval(timer);
          }
        }, 1000);
      }
    },
    // 生成四位验证码
    createCode() {
      var code = '';
      var codeLength = 4;
      var codeChars = '0123456789';
      for (var i = 0; i < codeLength; i++) {
        var charNum = Math.floor(Math.random() * codeChars.length);
        code += codeChars.charAt(charNum);
      }
      this.code = code;
    },
    // 发送邮箱验证码
    getEmailCode() {
      const email = this.form.email
      sendEmailApi({
        email,
        event: 'findpwd'
      }).then(res => {
        ElNotification({
          message: res.msg,
        });
        this.form.code=res.data.code
        console.log(this.form.code)
      })
    },

      /* 获取配置 */
      getSetting(){
      settingApi().then(res=>{
         this.logo=res.data.logo
      })
    },

    // 提交
    submit() {
      if (!this.inputCode) {
        ElNotification({
          title: 'Error',
          message: this.$t('请输入验证码'),
          type: 'error',
        });
        return;
      }
      if (this.inputCode != this.code) {
        ElNotification({
          title: 'Error',
          message: this.$t('验证码错误'),
          type: 'error',
        });
        return;
      }
      if (this.form.password != this.password) {
        ElNotification({
          title: 'Error',
          message: this.$t('请确保确认密码正确'),
          type: 'error',
        });
        return;
      }
      userFindApi(this.form).then(res => {
        // 提示找回密码
        ElNotification({
          message: res.msg,
          duration: 1000,
          onClose: () => {
            if (res.code == 1) {
              // 跳转到/dashboard页面
              this.$router.push('/login');
            }
          }
        })
      })
    }

  },
  mounted() {
    this.createCode()
    this.getSetting()
  }
}
</script>

<style lang="scss" scoped>
.bg {
  width: 100%;
  height: 100vh;
  background-image: url(../../assets/imgs/bg.jpg);
  background-size: 100% 108%;
  display: flex;
  justify-content: center;
  align-items: center;
  padding-top: 130px;
  box-sizing: border-box;
  position: relative;

  .find {
    position: absolute;
    width: 350px;
    height: 50px;
    background-color: #282b37;
    top: 110px;
    left: calc((100% - 350px)/2);
    display: flex;
    justify-content: center;
    align-items: center;
    border-radius: 8px;
  }

  .form {
    padding: 24px;
    box-sizing: border-box;
    background-color: #282b37;
    height: 525px;
    width: 350px;
    border-radius: 15px;

    .logo {
      margin-bottom: 20px;

      img {
        width: 302px;
        height: 72px;
      }
    }

    .box1 {
      .code {
        font-family: Arial;
        font-style: italic;
        color: blue;
        font-size: 20px;
        border: 0;
        padding: 2px 0;
        font-weight: bolder;
        cursor: pointer;
        width: 80px;
        height: 30px;
        line-height: 30px;
        text-align: center;
        background-color: #ffffff;
      }

      .inner {
        margin-bottom: 15px;
      }

      .inner6 {
        display: flex;
        justify-content: space-between;
        align-items: center;
      }

      .inner7 {
        display: flex;
        justify-content: center;
        align-items: center;
      }

      a {
        text-decoration: none;
      }
    }
  }
}
</style>
